<template>
	<view class="ts-column ts-flex-item">
		<ts-banner :image="config.cover_image" height="250"></ts-banner>

		<view class="ts-row">
			<view class="ts-row ts-flex-item" @tap="actionSheetStoreTypeTap">
				<ts-button type="primary">{{storeType}}</ts-button>
			</view>
			<view class="ts-row ts-flex-item" @tap="chooseAddress">
				<ts-button type="success">切换小区</ts-button>
			</view>


			<view class="ts-row ts-flex-item">
				<ts-button type="danger">全部类型</ts-button>
			</view>
		</view>

		<ts-search-bar v-model="keywords" @search="doSearch"></ts-search-bar>

		<view class="address-info">
			<text class="ts-text-bold ts-h5">{{address}}</text>
		</view>

		<view class="ts-column ts-flex-item">
			<view v-for="(item,index) in list" :key="index" class="ts-column ts-flex-item" @tap="openDetail(item.id)"
			 hover-class="navigator-hover">
				<view class="ts-column ts-padding">
					<image :src="item.cover_image" style="width: 100%; height: 250upx;"></image>
					<view class="ts-row" style="justify-content: space-between;">
						<text class="ts-h3">{{item.title}}</text>
						<view>
							<ts-badge type="danger" :text="item.type"></ts-badge>
						</view>
					</view>
					<view class="ts-row">
						<text>{{item.open_time}}</text>
					</view>
					<view class="ts-row">
						<text>{{item.service}}</text>
					</view>
					<view class="ts-row">
						<text>{{item.address}}</text>
					</view>
				</view>
				<view class="ts-gap"></view>
			</view>


		</view>

		<view class="ts-column">
			<ts-load-more :loadingType="loadingType" :contentText="contentText" @tap="getStoreListMoreData"></ts-load-more>
		</view>

	</view>
</template>

<script>
	import tsBanner from '@/components/teaset/components/ts-banner.vue';
	import tsSearchBar from '@/components/teaset/components/ts-search-bar.vue'

	export default {
		components: {
			tsBanner, //ts-banner
			tsSearchBar,
		},
		data() {
			return {
				storeType: '全部类别',
				keywords: '',
				list: [],
				config: {},
				page: 0,
				loadingType: 0, //上拉的状态：0-loading前；1-loading中；2-没有更多了
				address: '我的位置',
				location: {
					latitude: 0,
					longitude: 0
				},
			}
		},
		onLoad() {

			this.getAppConfig();

			uni.getLocation({
				type: 'gcj02',
				success: (res) => {
					console.log('当前位置的经度：' + res.longitude);
					console.log('当前位置的纬度：' + res.latitude);

					this.location = {
						longitude: res.longitude,
						latitude: res.latitude,
					}

					this.doSearch();
				}
			});
		},

		onReachBottom() {
			this.getStoreListMoreData();
		},
		methods: {
			openDetail(id) {
				uni.navigateTo({
					url: `home?id=${id}`,
				})
			},
			actionSheetStoreTypeTap() {
				const itemList = ['全部类别', '水果店', '菜篮子', '士多店', '快餐店', '其它'];
				uni.showActionSheet({
					itemList: itemList,
					success: e => {
						// console.log(JSON.stringify(e));
						this.storeType = itemList[e.tapIndex];
						this.doSearch();
					}
				});
			},

			chooseAddress() {
				uni.chooseLocation({
					success: (res) => {

						console.log(JSON.stringify(res));

						this.address = res.address;
						// 						console.log('位置名称：' + res.name);
						// 						console.log('详细地址：' + res.address);
						// 						console.log('纬度：' + res.latitude);
						// 						console.log('经度：' + res.longitude);

						this.location = {
							latitude: res.latitude,
							longitude: res.longitude
						};
						this.doSearch();
					}
				});
			},

			doSearch() {
				this.list = [];
				this.page = 0;
				this.getStoreListMoreData();
			},
			getAppConfig() {
				const api = this.$API_BASE + '/app/store/getAppConfig';
				uni.request({
					url: api,
					method: 'GET',
					dataType: 'json',
					data: {

					},
					success: (response) => {
						const res = response.data;
						console.log(JSON.stringify(res));
						// 						{
						// 							success:true,
						// 							message:'',
						// 							data:{}
						// 						}

						if (res.success) {
							this.config = res.data;
						} else {
							console.log(res.message);
						}
					}
				})
			},
			getStoreListMoreData() {

				if (this.loadingType != 0) {
					return;
				}

				this.page = this.page + 1;
				this.loadingType = 1;

				const api = this.$API_BASE + '/app/store/getStoreList';
				uni.request({
					url: api,
					method: 'GET',
					dataType: 'json',
					data: {
						keywords: this.keywords,
						page: this.page,
						type: this.storeType,
						location: this.location,
						distance: 1000,
					},
					success: (response) => {
						const res = response.data;

						console.log(JSON.stringify(res));
						// 						{
						// 							success:true,
						// 							message:'',
						// 							has_more:true,
						// 							data:[]
						// 						}

						if (res.success) {
							this.list = this.list.concat(res.data);

							if (res.has_more) {
								this.loadingType = 0;
							} else {
								this.loadingType = 2;
							}
						} else {
							console.log(res.message);
						}
					}
				})
			},
		},
	}
</script>

<style>
	page view {
		/* 		border: #000000 solid 1px;
		margin: 5upx; */
	}

	.ts-column {
		display: flex;
		flex-direction: column;
	}

	.ts-flex-item {
		flex: 1;
	}

	.address-info {
		font-size: 25upx;
		padding: 30upx 10upx;
		flex-direction: row;
		align-items: center;
	}
</style>
